package com.xinqi.modules.sys.textbook.manager.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.modules.sys.textbook.domain.CloudTextbookCatalogEntity;
import com.xinqi.modules.sys.textbook.dto.req.CloudTextbookCatalogQueryDTO;
import com.xinqi.modules.sys.textbook.dto.rsp.CloudTextbookCatalogResultDTO;
import com.xinqi.modules.sys.textbook.manager.CloudTextbookCatalogManager;
import com.xinqi.modules.sys.textbook.mapper.CloudTextbookCatalogMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * 云课本章节目录管理层实现
 *
 * @author: linkai
 * @date: 2023/04/25
 */
@Service
@RequiredArgsConstructor
public class CloudTextbookCatalogManagerImpl extends BaseServiceImpl<CloudTextbookCatalogMapper, CloudTextbookCatalogEntity> implements CloudTextbookCatalogManager {

    /**
     * 自定义分页查询
     */
    @Override
    public Page<CloudTextbookCatalogResultDTO> findPage(Page<Object> page, CloudTextbookCatalogQueryDTO query) {
        return this.baseMapper.findPage(page, query);
    }

    /**
     * 自定义列表查询
     */
    @Override
    public List<CloudTextbookCatalogResultDTO> findList(CloudTextbookCatalogQueryDTO query) {
        return this.baseMapper.findList(query);
    }

    /**
     * 根据id查询 云课本章节目录数据
     */
    @Override
    public CloudTextbookCatalogEntity findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return getById(id);
    }

    /**
     * 根据id删除 云课本章节目录数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return this.removeById(id);
    }

    @Override
    public List<CloudTextbookCatalogEntity> findByPid(Long pid) {
        LambdaQueryWrapper<CloudTextbookCatalogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudTextbookCatalogEntity::getPid, pid);
        return list(wrapper);
    }

    @Override
    public Boolean isExistName(Long textbookId, String name, Long pId, Long oId) {
        LambdaQueryWrapper<CloudTextbookCatalogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudTextbookCatalogEntity::getCloudTextbookId, textbookId);
        wrapper.eq(CloudTextbookCatalogEntity::getName, name);
        wrapper.eq(CloudTextbookCatalogEntity::getPid, pId);
        wrapper.ne(oId != null, CloudTextbookCatalogEntity::getId, oId);
        return count(wrapper) > 0;
    }

    @Override
    public boolean isExist(Long textbookId, Long id) {
        LambdaQueryWrapper<CloudTextbookCatalogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudTextbookCatalogEntity::getCloudTextbookId, textbookId);
        wrapper.eq(CloudTextbookCatalogEntity::getId, id);
        return count(wrapper) > 0;
    }

    @Override
    public boolean isExistChild(Long id) {
        LambdaQueryWrapper<CloudTextbookCatalogEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CloudTextbookCatalogEntity::getPid, id);
        return count(wrapper) > 0;
    }
}
